package ca.uwindsor;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class EditAssm extends Activity {

	String type;
	String course_code;
	
	// create an array adapter
	ArrayAdapter<CharSequence> adapter = null;
	
	//TODO: get id from view schedule
	int _id;
	public void set_id(int _id) {
		this._id = _id;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_assm);
		
		//get id from view schedule
		Intent intent = getIntent();
		set_id(intent.getExtras().getInt("_id"));
		
		
		//Change title and button text.
		
		//Populate course code spinner
		// TODO: change this value to the global term value

		
		
		//Change title and button text.
		Button add_but = (Button) findViewById(R.id.add_button);
		TextView txtTitle = (TextView)findViewById(R.id.txtTitle);
		
		add_but.setText("Edit");
		txtTitle.setText("Edit Schedule");
		
		//TODO: get values from table by id and populate fields
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("event_schedule");
		
		db.open();
		
		Cursor ret = db.getEntry("_id", _id);
		
		Common c = new Common();
		
		Date dbDate = null;
		Date eventStartTime = null;
		Date eventEndTime = null;
		
		//TODO: get details from table.
		if(ret.moveToFirst()){
			
			course_code = ret.getString(2);
			
			fillSpinner();
			
			dbDate = c.StringToDate(ret.getString(1));
			eventStartTime = c.StringToTime(ret.getString(3));
			eventEndTime = c.StringToTime(ret.getString(4));
			
		
		//TODO: set values on form
		
		TimePicker startTime = (TimePicker)findViewById(R.id.startTime);
		TimePicker endTime = (TimePicker)findViewById(R.id.endTime);
		
		startTime.setCurrentHour(eventStartTime.getHours());
		startTime.setCurrentMinute(eventStartTime.getMinutes());
		
		endTime.setCurrentHour(eventEndTime.getHours());
		endTime.setCurrentMinute(eventEndTime.getMinutes());
		
		
		
		DatePicker eventDate = (DatePicker) findViewById(R.id.eventDate);
		
		eventDate.init(dbDate.getYear()+1900,dbDate.getMonth(),dbDate.getDate(),null);
		
		EditText comments = (EditText)findViewById(R.id.txtComments);
		comments.setText(ret.getString(6));
		
		Spinner sType = (Spinner) findViewById(R.id.asmSpinnerType);
		Spinner sCourseCode = (Spinner) findViewById(R.id.asmSpinnerCode);
		
		//Set default value from db
		if(ret.getString(5).equals("Assignment")){
			sType.setSelection(0);
		}else{
			sType.setSelection(1);
		}
		//get course position from array
		int coursePos = adapter.getPosition(ret.getString(2));
		
		sCourseCode.setSelection(coursePos);
		
		
		sCourseCode.setOnItemSelectedListener(new MyOnItemSelectedListener());
		sType.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
		}
		db.close();
		// Alert Dialog builder
				final AlertDialog.Builder alert = new AlertDialog.Builder(this);
				
				add_but.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
						db.setDATABASE_TABLE("event_schedule");
						
						db.open();
						
						//TODO: get values from form
						
						TimePicker startTime = (TimePicker)findViewById(R.id.startTime);
						TimePicker endTime = (TimePicker)findViewById(R.id.endTime);
						
						String txtStartTime = startTime.getCurrentHour().toString()
												+":"+startTime.getCurrentMinute().toString();
						
						String txtEndTime = endTime.getCurrentHour().toString()
											+":"+endTime.getCurrentMinute().toString();
						
						DatePicker eventDate = (DatePicker) findViewById(R.id.eventDate);
						String eventDateStr = eventDate.getYear() + "-"
								+ (eventDate.getMonth()+1) + "-" + eventDate.getDayOfMonth();
						
						EditText comments = (EditText)findViewById(R.id.txtComments);
						/*String where = "sem_name" + "= '" + Common.globalTerm+"' and "
										+ "course_code" + "= '" + course_code+"' and "
										+ "Day" + "= '" + day+"' and "
										+"StartTime" + "= '" +txtStartTime +"' and "
										+"EndTime" + "= '" +txtEndTime +"'";
						Cursor ret = db.getEntry(where);*/
						
						//if(ret.getCount()==0){
							//add course
						if(new Common().CompareTime(txtEndTime, txtStartTime)<=0)
						{
							alert.setMessage("End time should be greater than start time");
							alert.setNegativeButton("OK", new OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
								}

							});
							alert.show();
						}
						else{			
							ContentValues newValues = new ContentValues();
			
							newValues.put("Date", eventDateStr);
							
							newValues.put("course_code", course_code);
							
							newValues.put("StartTime", txtStartTime);
							
							newValues.put("EndTime", txtEndTime);
							
							newValues.put("Type", type);
							
							newValues.put("Comments", comments.getText().toString());
							
														
						    //TODO: get id and pass that value in
							db.updateEntry("_id", _id, newValues);
							
							alert.setMessage("Schedule added for course "+course_code);
							
							alert.setNegativeButton("OK", new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									startActivity(new Intent("ca.uwindsor.VIEWSCHEDULE"));
								}

							});
							alert.setCancelable(false);
							alert.show();
			
						db.close();
					}
					}
				});
		
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
	
	public void fillSpinner()
	{
		
		adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		adapter.add(course_code);
		Spinner s = (Spinner) findViewById(R.id.asmSpinnerCode);
		s.setAdapter(adapter);

	}
	
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			// System.out.print(parent.getAdapter().toString());
			switch (parent.getId()) {
			case R.id.asmSpinnerCode:
				course_code = parent.getItemAtPosition(pos).toString();
				break;
			case R.id.asmSpinnerType:
				type = parent.getItemAtPosition(pos).toString();
				break;

			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}
	
}
